/**
 * Copyright 2008-2009 HAN.BIFANG
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 */
package han.bifang.gwt.server.action.stat;

import han.bifang.business.service.ServiceManager;
import han.bifang.business.stat.StatManager;
import han.bifang.common.entity.SoaLogDetail;
import han.bifang.common.entity.SoaLogErrorInfo;
import han.bifang.common.entity.SoaRetryRecord;
import han.bifang.common.exception.BifangErrorCode;
import han.bifang.common.exception.BifangException;
import han.bifang.gwt.client.common.RequestObject;
import han.bifang.gwt.client.common.ResponseObject;
import han.bifang.gwt.client.common.entity.LogErrorInfo;
import han.bifang.gwt.client.common.entity.NormalizedMessage;
import han.bifang.gwt.client.common.entity.RetryRecord;
import han.bifang.gwt.server.action.BifangGWTAction;

import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.BeanUtils;

import v1.message.bashe.han.BasheMessageDocument;

/**
 * 
 * @Author Kent
 */
public class LogDetailAction implements BifangGWTAction {

	@Override
	public ResponseObject execute(RequestObject req,
			HttpServletRequest request, HttpServletResponse response)
			throws BifangException {
		ResponseObject res = new ResponseObject();
		try {
			Object cmd = (Object) req.getRequest();
			if (cmd instanceof String) {
				if (cmd.equals("request")) {
					SoaLogDetail me = (SoaLogDetail) request.getSession()
							.getAttribute("log");
					res.setCode("0");
					if (me.getRequest() != null) {
						BasheMessageDocument doc = BasheMessageDocument.Factory
								.parse(me.getRequest());
						NormalizedMessage nm = InternalConverter
								.bashe2Normalized(doc);

						res.setResponse(nm);
					} else {
						res.setResponse(null);
					}
				} else if (cmd.equals("response")) {
					SoaLogDetail me = (SoaLogDetail) request.getSession()
							.getAttribute("log");
					res.setCode("0");

					if (me.getResponse() != null) {
						BasheMessageDocument doc = BasheMessageDocument.Factory
								.parse(me.getResponse());
						NormalizedMessage nm = InternalConverter
								.bashe2Normalized(doc);

						res.setResponse(nm);
					} else {
						res.setResponse(null);
					}
				} else if (cmd.equals("error")) {
					SoaLogDetail me = (SoaLogDetail) request.getSession()
							.getAttribute("log");
					res.setCode("0");
					if (me.getErrors().size() > 0) {
						SoaLogErrorInfo e = (SoaLogErrorInfo) me.getErrors()
								.get(0);
						LogErrorInfo le = new LogErrorInfo();
						BeanUtils.copyProperties(le, e);
						res.setResponse(le);
					} else
						res.setResponse(null);
				} else if (cmd.equals("retry")) {
					SoaLogDetail me = (SoaLogDetail) request.getSession()
							.getAttribute("log");
					System.out.println(me.getId());
					// refresh the message change data in case of new retry
					// occurs
					me = StatManager.getMessageExchange(me.getId());
					request.getSession().setAttribute("log", me);
					res.setCode("0");
					if (me.getRetryRecord() != null
							&& me.getRetryRecord().size() > 0) {
						ArrayList<RetryRecord> ret = new ArrayList<RetryRecord>();
						for (SoaRetryRecord r : me.getRetryRecord()) {
							RetryRecord rr = new RetryRecord();
							BeanUtils.copyProperties(rr, r);

							ret.add(rr);
						}
						res.setResponse(ret);
					} else
						res.setResponse(null);
				}
			} else {
				ArrayList<String> l = (ArrayList<String>) cmd;
				SoaLogDetail me = StatManager.getMessageExchange(l.get(0));
				request.getSession().setAttribute("log", me);
				Boolean canRetry = ServiceManager.canRetry(l.get(1), l.get(2));

				res.setCode("0");
				res.setResponse(canRetry);
			}

		} catch (BifangException e) {
			throw e;
		} catch (Exception e) {
			throw new BifangException(BifangErrorCode.UNKNOWN_ERROR, e
					.getMessage(), e);
		}
		return res;
	}
}
